{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "from scipy.stats import chi2_contingency"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "data = pd.read_excel('检验数据.xlsx')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [],
   "source": [
    "Age = data['2.请问您的年龄是?']\n",
    "ChannelA = data['7.请问您是通过什么渠道了解到AI对话助手的?(A. 视频媒体（抖音、快手等视频平台）)']\n",
    "ChannelB = data['7 (B.社交媒体（微信、QQ等）)']\n",
    "ChannelC = data['7 (C.传统媒体（报纸、杂志、电视等）)']  \n",
    "ChannelD = data['7 (D. 朋友推荐)']\n",
    "ChannelE = data['7 (E.浏览器搜索)']\n",
    "ChannelF = data['7 (F.新闻推送)']\n",
    "ChannelG = data['7 (G.很少关注)']\n",
    "ChannelsName = ['视频媒体', '社交媒体', '传统媒体', '朋友推荐', '浏览器搜索', '新闻推送', '很少关注']\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2.请问您的年龄是?  1   2   3   4\n",
      "视频媒体        8  61  50  12\n",
      "社交媒体        9  47  40   7\n",
      "传统媒体        1  15  12   3\n",
      "朋友推荐        4  27  14   5\n",
      "浏览器搜索       3  23  18   7\n",
      "新闻推送        3  40  23   3\n",
      "很少关注        0   0   0   0\n"
     ]
    }
   ],
   "source": [
    "# 进行年龄与了解渠道的独立性检验\n",
    "# 构建列联表，年龄有一列四个值，了解渠道有七列，每列有两个值\n",
    "\n",
    "# 假设 \"Age\" 是年龄列，\"ChannelA\" ~ \"ChannelG\" 是了解渠道列\n",
    "age_column = Age\n",
    "channel_columns = [ChannelA, ChannelB, ChannelC, ChannelD, ChannelE, ChannelF, ChannelG]\n",
    "\n",
    "# 将数据从长格式转换为适合列联表的格式\n",
    "contingency_table = pd.DataFrame()\n",
    "\n",
    "for i, channel in enumerate(channel_columns):\n",
    "    channel_data = channel.groupby(age_column).sum()  # 统计不同年龄段在该渠道的总人数\n",
    "    contingency_table[ChannelsName[i]] = channel_data\n",
    "\n",
    "# 转置表格，使了解渠道为行名，年龄为列名\n",
    "contingency_table = contingency_table.T\n",
    "\n",
    "# 显示结果\n",
    "print(contingency_table)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "卡方值: 9.12445925268275\n",
      "p值: 0.9567894878604966\n",
      "接受原假设，即年龄与了解渠道独立\n"
     ]
    }
   ],
   "source": [
    "# 再对列联表进行卡方检验,输出卡方值和对应p值\n",
    "# 添加一个小常数到列联表以避免零频率\n",
    "contingency_table += 1e-10\n",
    "chi2, p, dof, expected = chi2_contingency(contingency_table)\n",
    "print('卡方值:', chi2)\n",
    "print('p值:', p)\n",
    "#给出判断结果\n",
    "if p < 0.05:\n",
    "    print('拒绝原假设，即年龄与了解渠道不独立')\n",
    "else:\n",
    "    print('接受原假设，即年龄与了解渠道独立')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
